import java.util.Scanner;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;

public class DTRS_Server {
	
	//private static String boundedName = "BoxOfficeServer_montreal";
	private static String boundedName = "montreal";
	
	public static void main(String args[]){
		try{
			System.out.print("Server name/location: ");
			Scanner s = new Scanner(System.in);
			boundedName = s.next();
			
			ORB orb = ORB.init(args, null);
			POA rootpoa = 
			(POA)orb.resolve_initial_references("RootPOA");
			rootpoa.the_POAManager().activate();
			
			BoxOfficeServer_Impl servantImpl = new BoxOfficeServer_Impl();
			servantImpl.setORB(orb);
			
			org.omg.CORBA.Object ref = 
			rootpoa.servant_to_reference(servantImpl);
			BoxOfficeServer_interface bref = BoxOfficeServer_interfaceHelper.narrow(ref);
			
			org.omg.CORBA.Object objRef = 
			orb.resolve_initial_references("NameService");
			
			NamingContextExt ncRef = 
			NamingContextExtHelper.narrow(objRef);
			
			//String boundedName = "BoxOfficeServer_montreal";
			//String name = "Hello";
			NameComponent path[] = ncRef.to_name(boundedName);
			ncRef.rebind(path, bref);
			
			System.out.println(boundedName + "-Box-Office-Server is ready and waiting...");
			
			//activate the server to listen to other DTRS servers
			servantImpl.enableListener();
			orb.run();
		
			
		}
		catch(Exception exc){
			System.out.println("Something wrong in server!");
		}
	}

}
